# -*- coding: utf-8 -*-
import requests
import json
from pyquery import PyQuery as jq
url = "http://coral.qq.com/article/2635557107/comment/v2?callback=_article2619799396commentv2&orinum=10&oriorder=o&pageflag=1&cursor=0&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=1"

url2 = 'https://coral.qq.com/article/batchcommentnum?targetid=2623970564&callback=_cbSum&_=1525660316438'

url3 = "https://news.qq.com/a/20180503/002398.htm"
headers = json.loads(r'''{
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
    "Connection": "keep-alive",
    "Cookie": "RK=0bkivATLQP; pgv_pvi=3636279296; eas_sid=Q1c5E0b4l2m7c9W7e986b7d698; tvfe_boss_uuid=c770c9b965b339f0; ptcz=5168196bb1e309f87cb2f68fee1054c28f5ca2978bc4d187691444c1bcc5871b; pt2gguin=o0824300438; pgv_pvid=2783642000; o_cookie=824300438; ptag=www_baidu_com|/; pgv_info=ssid=s6974547792; ts_refer=www.baidu.com/link; ts_uid=7544839156; ts_last=news.qq.com/world_index.shtml; ad_play_index=93; roll_mod=1",
    "Host": "coral.qq.com",
    "Referer": "http://roll.news.qq.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}''')
# print(requests.get(url,headers=headers).text)

# resp = requests.get(url2,headers=headers).text
# try:
# 	myjson = json.loads("".join("".join(resp.split("(")[1:]).split(")")[:-1]))
# except:
# 	raise ValueError('Invalid Input')
# print(myjson['data'][0]['commentnum'])

# print(jq(requests.get(url3).text)('.qq_main > .qq_article > .hd > .qq_bar.clearfix > .a_Info > .a_source > a').text())

# resp = requests.get("https://news.qq.com/a/20180503/002398.htm")
# lists = resp.text.split('cmt_id = ')
# # with open('ceshi.html','w',encoding='utf-8') as f:
# # 	f.write(resp.text)
# 	# json.dump(resp.text, f)
# if len(lists) > 1:
# 	aid = lists[1].split(';')[0]
# 	print(aid)
import pandas as pd
datacol = {0:'新闻标签',1:'新闻来源',2:'新闻发布时间',3:'评论数量',4:'新闻标题',5:'用户名',6:'用户IP',7:'发布时间',8:'顶(数量)',9:'评论内容',10:'回复'}
with open('newsgj.json','r',encoding='utf-8') as f:
	myjson = json.loads(f.read())
df = pd.DataFrame(myjson)
df.rename(columns=datacol,inplace=True)
df.to_excel('newsgj.xlsx')
with open('newsgn.json','r',encoding='utf-8') as f:
	myjson1 = json.loads(f.read())
df1 = pd.DataFrame(myjson1)
df1.rename(columns=datacol,inplace=True)
df1.to_excel('newsgn.xlsx')
with open('newssh.json','r',encoding='utf-8') as f:
	myjson2 = json.loads(f.read())
df2 = pd.DataFrame(myjson2)
df2.rename(columns=datacol,inplace=True)
df2.to_excel('newssh.xlsx')



